
// 编辑
function editClass_2() {
	$('#tab-edit').show();
	$('#tab-edit').click();
}
// $(".removeClass").on('click', function() {
// removeClass(this)
// });

$(".btn-bottm").click(function(){
	$("#tab-list").parent().attr("class","active");
	$("#tab-add").parent().removeClass("active");
	$("#tab-edit").parent().removeClass("active");
	$("#tab-edit").css("display","none");
	$("#home").addClass("active");
	$("#profile").removeClass("active");
	$("#editUser").removeClass("active");
});
// 删除
function removeClass(_this) {
	$(_this).parent().parent().remove();
}
$('#tab-add,#tab-list').click(function() {
	$("#preId").val("-1");
	$("#pId").val("");
	$('#tab-edit,#tab-allocate').hide();
})
// 缩略图标
$(".fa-hover").click(function() {
	var icon = $(this).find("i").attr("class");
	$('i#menu-icon').removeClass().addClass(icon).text(icon.substr(2));
	$('i#menu-icon2').removeClass().addClass(icon).text(icon.substr(2));
	$("#iconAdd").val(icon.substr(2));
	$("#icon2").val(icon.substr(2));
	$("#selectIcon1").modal('hide');
});

$(".insert").click(function() {
	if(!$("#name").val()){
		toastr.error('请输入栏目名称!');
		return false;
	}
	$.ajax({
		url : ctx + '/cms/saveColumn',
		type : 'POST',
		data : $("#insert").serialize(),
		dataType : 'text',
		success : function(data) {
			if(data > 0){
				toastr.success('添加成功!');
				window.location.href=ctx+"/cms/column"; 
			}else{
				toastr.error('添加失败!');
			}
		},
		error : function(msg) {
			toastr.error('系统错误!');
		}
	});
});

$(".edit").click(function(){
	$.ajax({
		url : ctx + '/cms/editColumn',
		type : 'POST',
		data : $("#edit").serialize(),
		dataType : 'text',
		success : function(data) {
			if(data > 0){
				toastr.success('修改成功!');
			}else{
				toastr.error('修改失败!');
			}
		},
		error : function(msg) {
			toastr.error('系统错误!');
		}
	});
});

function delClass(id){
	$.ajax({
		url : ctx + '/cms/delColumn',
		type : 'POST',
		data : {id:id},
		dataType : 'json',
		success : function(data) {
			if(data > 0){
				toastr.success('删除成功!');
				window.location.href=ctx+"/cms/column"; 
			}else{
				toastr.error('删除失败!');
			}
		},
		error : function(msg) {
			toastr.error('系统错误!');
		}
	});
}

$(".indenter").click(function(){
	var id = $(this).parent().parent().attr("data-tt-id");
	//branch expanded
	if($(this).parent().parent().attr("class") == "branch expanded"){
		//若朝下变为超右
		$(this).parent().parent().attr("class","branch collapsed");
		$("tbody tr").each(function(){
			if($(this).attr("data-tt-parent-id") == id ){
				$(this).remove();
			}
		});
		
	}else{
		var _this = $(this);
		var html = "";
		$.ajax({
			url : ctx + '/cms/index',
			type : 'POST',
			data : {
				preId : id,
				enable:1
			},
			dataType : 'json',
			success : function(data) {
				$(data).each(function(i){
					var sort = data[i]["sort"];
					if(!sort){
						sort = '0';
					}
					
					html +='<tr data-tt-id='+data[i]["id"]+' data-tt-parent-id='+data[i]["preId"]
					+'><td><span class="indenter" style="padding-left: 23px;"></span><a href="#">'+data[i]["name"]+'</a></td><td>'+data[i]["model"]+'</td>'
					+'<td style="text-align: center;"><input name="sort" type="text" value='
					+sort+' style="width: 50px; margin: 0; padding: 0; text-align: center;"></td>'
					+'<td><a href="#" class="btn btn-info btn-xs editClass_2" onclick="editClass('+data[i]["id"]+')"> '
					+'<i class="fa fa-pencil"></i> 修改</a> <a href="#" onclick="delClass('+data[i]["id"]+')" class="btn btn-danger btn-xs"><i class="fa fa-trash-o"></i> 删除</a></td></tr>';
				})
				$(_this).parent().parent().attr("class","branch expanded").after(html);
			},
			error : function(msg) {
				toastr.error('树加载异常!');
			}
		});
	}
});

function editClass(id){
	$.ajax({
		url : ctx + '/cms/findColumnById',
		type : 'POST',
		data : {id:id},
		dataType : 'json',
		success : function(data) {
			$("#id2").val(data["id"]);
			$("#preId2").val(data["preId"]);
			$("#preName2").val(data["preName"]);
			$("#model2").val(data["model"]);
			$("#name2").val(data["name"]);
			$("#url2").val(data["url"]);
			$("#description2").val(data["description"]);
			$("#target2").val(data["target"]);
			$("#keyword2").val(data["keyword"]);
			$("#sort2").val(data["sort"]);
			$("#icon2").val(data["icon"]);
			$("#menu-icon2").attr("class","fa "+data["icon"]).text(data["icon"]);
			$("#editUser").find("input[type=radio][name='isComments'][value='"+data["isComments"]+"']").attr("checked",'checked');
			$("#editUser").find("input[type=radio][name='isAudit'][value='"+data["isAudit"]+"']").attr("checked",'checked');
			
			$('#tab-edit').show();
			$('#tab-edit').click();
		},
		error : function(msg) {
			toastr.error('系统错误!');
		}
	});
//	editClass_2(id);
}



// 上级管理
$(function() {
	var setting2 = {
		check : {
			enable : false
		},
		data : {
			simpleData : {
				enable : true
			}
		},
		callback : {
			onClick : onClick
		}
	};

	var zNodes2;
	var code2;
	function setCheck() {

		var zTree = $.fn.zTree.getZTreeObj("treeDemo"), py = $("#py").attr(
				"checked") ? "p" : "", sy = $("#sy").attr("checked") ? "s" : "", pn = $(
				"#pn").attr("checked") ? "p" : "", sn = $("#sn")
				.attr("checked") ? "s" : "", type = {
			"Y" : py + sy,
			"N" : pn + sn
		};
		zTree.setting.check.chkboxType = type;
		showCode('setting.check.chkboxType = { "Y" : "' + type.Y + '", "N" : "'
				+ type.N + '" };');
	}
	function showCode(str) {
		if (!code2)
			code2 = $("#code");
		code2.empty();
		code2.append("<li>" + str + "</li>");
	}
	function fuzhi(data) {
		zNodes2 = eval("(" + data + ")");
		$.fn.zTree.init($("#treeDemo"), setting2, zNodes2);
		setCheck();
		$("#py").bind("change", setCheck);
		$("#sy").bind("change", setCheck);
		$("#pn").bind("change", setCheck);
		$("#sn").bind("change", setCheck);

	}

	function onClick(event, treeId, treeNode, clickFlag) {
		$("#pId").val(treeNode.name);
		$("#preId").val(treeNode.id);
		$("#pId2").val(treeNode.name);
		$("#preId2").val(treeNode.id);
		$("#deparment").hide();
	}

	$(".deparment").click(function() {
		$("#deparment").show();
		$.ajax({
			url : ctx + '/cms/index',
			type : 'POST',
			data : {
				preId : '-1',
				enable: 1
			},
			dataType : 'text',
			success : function(data) {
				var _data = eval("(" + data + ")");
				_data.forEach(function(x,y){
					x['pId'] = x['preId'];
					delete x['preId'];
					delete x['isComments'];
					delete x['createTime'];
					delete x['enable'];
					delete x['isAudit'];
					delete x['icon'];
					delete x['description'];
					delete x['model'];
					delete x['keyword'];
					delete x['target'];
					delete x['url'];
					delete x['preName'];
				});
				$.fn.zTree.init($("#tree2"), setting2, _data);
			},
			error : function(msg) {
				toastr.error('树加载异常!');
			}
		});
	});

	$(".close,._close").click(function() {
		$("#deparment").hide();
	});

});
